package com.hqyj.springcloud.service.impl;

import com.hqyj.springcloud.mapper.OrderMapper;
import com.hqyj.springcloud.pojo.Order;
import com.hqyj.springcloud.pojo.RestfulEntity;
import com.hqyj.springcloud.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RestTemplate restTemplate;

    private String url = "http://SPRINGCLOUD-EUREKA-PAYMENT";

    @Override
    public RestfulEntity add(Order order){
        RestfulEntity result = null;
        // 创建订单对象
        int insertOrder = orderMapper.insertOrder(order);
        if (insertOrder > 0){
            // 调取支付模块
            ResponseEntity<RestfulEntity> restfulEntityResponseEntity = restTemplate.postForEntity(url+"/app", order, RestfulEntity.class);
            if (restfulEntityResponseEntity.getStatusCode().is2xxSuccessful()) {
                result = restfulEntityResponseEntity.getBody();
            }
        }
        return result;
    }
}
